# -*- coding: utf-8 -*-
import os
import shutil
import zipfile

def zip_dir(dirname,zipfilename=''):
    filelist = []
    if os.path.isfile(dirname):
        filelist.append(dirname)
    else :
        for root, dirs, files in os.walk(dirname):
            for name in files:
                filelist.append(os.path.join(root, name))
    if zipfilename=='':
        (dirname, curdirname) = os.path.split(dirname)
        zipfilename=os.path.join(dirname,curdirname+'.zip')
        if os.path.exists(zipfilename):
            zipfilename=os.path.join(dirname,curdirname+'(1).zip')
    zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED)
    for tar in filelist:
        # print(tar)
        arcname = tar[len(dirname):]
        # print (arcname)
        zf.write(tar,arcname)
    zf.close()

def  zip_dir_to_one(dirname, zipfilename=''):
    filelist = []
    if os.path.isfile(dirname):
        filelist.append(dirname)
    else:
        for root, dirs, files in os.walk(dirname):
            for name in files:
                filelist.append(os.path.join(root, name))
    if zipfilename=='':
        (filepath, zipfilename1) = os.path.split(dirname)
        zipfilename=filepath+'/'+zipfilename1+'.zip'
        if os.path.exists(zipfilename):
            zipfilename=filepath+'/'+zipfilename1+'(1).zip'
    zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED)
    for tar in filelist:
        # print(tar)
        # arcname = tar[len(dirname):]
        (filepath, tempfilename) = os.path.split(tar)
        # print(tempfilename)
        zf.write(tar, tempfilename)#tempfilename文件压缩到指定路径
    zf.close()
    return zipfilename


def un_zip_curdir(unzip_file_path):
    """
    解压文件到当前目录
    :param unzip_file_path:
    :return:
    """
    dst = os.path.dirname(unzip_file_path)
    un_zip(unzip_file_path, dst)
    # os.remove(unzip_file_path)

def un_zip(file_name,dst):
    """unzip zip file"""
    zip_file = zipfile.ZipFile(file_name)
    # if os.path.isdir(file_name + "_files"):
    #     pass
    # else:
    #     os.mkdir(file_name + "_files")
    # print(file_name)
    for item in zip_file.infolist():
        try:
            name1 = item.filename.encode('cp437').decode('gbk')
        except:
            name1 = item.filename.encode('utf-8').decode('utf-8')
        # print(name1)
        item.filename=name1
        zip_file.NameToInfo[item.filename] = item
    # print(zip_file.infolist())

    for names in zip_file.namelist():
        # print(names)
        zip_file.extract(names,dst)

        # print(name1)
        # print(os.path.join(dst, names))
        # print(os.path.join(dst, name1))
        # os.rename(dst+"\\"+names, dst+"\\"+name1)
        # shutil.move(dst+"\\"+names, dst+"\\"+name1)

    zip_file.close()


if __name__ == '__main__':
    # s=zip_dir_to_one('E:\\python\\project\\FFAutoTest\\TestResult\\2018-05-07_11_14_5740')
    # print(s)
    # un_zip('E:\\python\\project\\FFAutoTest\\TestResult\\2018-04-26_10_37_1057(1).zip','E:\\python')
    un_zip_curdir('D:\\测试\\嗯嗯.zip')
    # (filepath, tempfilename) = os.path.split('E:\\python\\project\\FFAutoTest\\TestResult\\2018-04-26_10_37_1057')
    pass
    # print(tempfilename)